#!/bin/sh

######################################
# 用法
######################################

usage() {
cat <<EOF
Usage: sh $0 command [argument]

command:
  clean:           Cleans Up Project Folder
  build:           Builds mq broker with release configuration
  test:            Tests mq broker with release configuration
  run:             Cleans, builds, than runs mq consumer 
  stop:            Stops current mq consumer if exists.
  test:            Tests all cases.
EOF
}

######################################
#启动文件路径
######################################
MQBROKER_PATH=/home/ubuntu/go/src/trustmq_consumer

######################################
# 输入验证
######################################

if [ "$#" -eq 0 -o "$#" -gt 1 ]; then
    usage
    exit 1
fi

COMMAND="$1"

case "$COMMAND" in

    ######################################
    # Clean
    ######################################
    "clean")
		go clean
        exit 0
        ;;
    "build")
	    go build $MQBROKER_PATH/mqconsumer.go
    	exit 0
    	;;
    "run")
	    sh $MQBROKER_PATH/build.sh stop
    	sh $MQBROKER_PATH/build.sh clean
    	sh $MQBROKER_PATH/build.sh build    	
    	str=$"\n"
    	nohup $MQBROKER_PATH/trustmq_consumer >$MQBROKER_PATH/trustmq_consumer.log &
    	sstr=$(echo -e $str)
		echo $sstr
		tail -20 $MQBROKER_PATH/trustmq_consumer.log
    	exit 0
    	;;
    "stop")
        pkill $MQBROKER_PATH/trustmq_consumer
    	exit 0    	
    	;;
    "test")
    	go test
    	exit 0
    	;;

esac



